#!/bin/bash

# 测试多服务容器
echo "Testing multi-service container..."

# 等待容器启动
echo "Waiting for container to start..."
sleep 10

# 测试MySQL连接
echo "Testing MySQL connection..."
if mysql -h localhost -P 3306 -u root -p123456 -e "SELECT 1;" > /dev/null 2>&1; then
    echo "✅ MySQL connection successful"
else
    echo "❌ MySQL connection failed"
fi

# 测试Redis连接
echo "Testing Redis connection..."
if redis-cli -h localhost -p 6379 -a 123456 ping > /dev/null 2>&1; then
    echo "✅ Redis connection successful"
else
    echo "❌ Redis connection failed"
fi

# 测试MySQL功能
echo "Testing MySQL functionality..."
mysql -h localhost -P 3306 -u root -p123456 -e "
    CREATE DATABASE IF NOT EXISTS test_db;
    USE test_db;
    CREATE TABLE IF NOT EXISTS test_table (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100),
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    INSERT INTO test_table (name) VALUES ('test_user');
    SELECT * FROM test_table;
    DROP DATABASE test_db;
" 2>/dev/null

if [ $? -eq 0 ]; then
    echo "✅ MySQL functionality test passed"
else
    echo "❌ MySQL functionality test failed"
fi

# 测试Redis功能
echo "Testing Redis functionality..."
redis-cli -h localhost -p 6379 -a 123456 << EOF
SET test_key "test_value"
GET test_key
DEL test_key
EOF

if [ $? -eq 0 ]; then
    echo "✅ Redis functionality test passed"
else
    echo "❌ Redis functionality test failed"
fi

# 显示容器状态
echo ""
echo "Container status:"
docker ps | grep multi-service

# 显示容器日志
echo ""
echo "Container logs (last 20 lines):"
docker logs --tail 20 multi-service-demo

echo ""
echo "Multi-service container test completed!" 